%% On the Optimal Design of Transfers and Income-Tax Progressivity
% Tax rates for the parameterized version of the analytical model

clear; clc; close all;
addpath('../Matlab_Figure_Files');

%% Grids

ylb = -0.00;                    % lower bound for labor income grid
yub = 3;                        % upper bound for labor income grid 
Ny = 3001;                      % number of pounts in labor income grid
yvec = linspace(ylb,yub,Ny)';   % labor income grid
yk = 0.00;                      % capital income (no capital in this model)

ymean = 0.3;                    % calibrated mean income in analytical model

%% Parameters for transfers

rt = 0.0000;    % phase-out speed of transfer (only lump sum transfers in the analytical model)
mt = 0.070714;  % size of the transfer
ot = 0.0000;    % phase-out threshold of transfer (only lump sum transfers in the analytical model)

% Auxiliary variables for transfer
et  = exp(-rt*(yvec-ot));
ct  = exp(rt*ot)/(1+exp(rt*ot));

% Transfer function
tr  = -mt*et./(1+et)*(1/ct);

%% Parameters for tax (with transfer)

% Parameters
gamma = -0.095636;   % progressivity parameter
lambda = 0.57445;    % level parameter

% HSV
ta = (1 - lambda*yvec.^(-gamma)).*yvec;

% Total tax function
tt = ta + tr; 

%% Computing the marginal tax rate

% HSV
mta = 1 - lambda.*(1-gamma).*yvec.^(-gamma);

% Marginal tax from transfer
mtr = mt*rt* (et./((1+et).^2)) *(1/ct);

% Total marginal tax rate
mtt = mta + mtr;

%% HSV only

% no transfer

% Parameters for tax
lambda = 0.58525;   % income tax level 
gamma = 0.23579;    % income tax progressivity 

% HSV
ta_hsv = (1 - lambda*yvec.^(-gamma)).*yvec;

% Total tax function
tt_hsv = ta_hsv; 

% Marginal tax rate
mta_hsv = 1 - lambda.*(1-gamma).*yvec.^(-gamma);

% Total marginal tax rate
mtt_hsv = mta_hsv;


%% Add the Mirrlees

load Results_case1_benchmark 
ymean_mirrlees = 0.944547882542137;

%% Plots

% Color
fig = figure(101);
subplot(1,2,1)
plot(yvec/ymean,ta./yvec,'--','linewidth',2); hold on
plot(yvec/ymean,tr./yvec,':','linewidth',2); hold on
plot(yvec/ymean,tt./yvec,'-','linewidth',2);  hold on
plot(yvec/ymean,tt_hsv./yvec,'-.','Color',[0.5,0.5,0.5],'linewidth',2); hold on
plot(sb.y(1:end-1000)./ymean_mirrlees,sb.ATR(1:end-1000),'-.','linewidth',2,'Color',[0.4940,0.1840,0.5560]); 
xlim([0 3.0])
ylabel('Average rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12)
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.7  0.7])

subplot(1,2,2)
plot(yvec/ymean,mta,'--','linewidth',2); hold on
plot(yvec/ymean,mtr,':','linewidth',2); hold on
plot(yvec/ymean,mtt,'-','linewidth',2);  hold on
plot(yvec/ymean,mtt_hsv,'-.','Color',[0.5,0.5,0.5],'linewidth',2); hold on
plot(sb.y(1:end-1000)./ymean_mirrlees,sb.MTR(1:end-1000),'-.','linewidth',2,'Color',[0.4940,0.1840,0.5560])
xlim([0 3.0])
leg = legend('Tax','Transfer','Total $t\&T$ rate','No-transfer','Mirrlees');
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12)                
legend boxoff
ylabel('Marginal rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.7  0.7])

run graph_extended.m;
saveas(gcf,'am_rates_SB.pdf')

% Black-white
fig = figure(102);
subplot(1,2,1)
plot(yvec/ymean,ta./yvec,'--','Color',[0 0 0],'linewidth',2); hold on
plot(yvec/ymean,tr./yvec,':','Color',[0 0 0],'linewidth',2); hold on
plot(yvec/ymean,tt./yvec,'-','Color',[0 0 0],'linewidth',2);  hold on
plot(yvec/ymean,tt_hsv./yvec,'-.','Color',[0.4,0.4,0.4],'linewidth',2,'MarkerIndices',1:50:length(yvec)); hold on
plot(sb.y(1:end-1000)./ymean_mirrlees,sb.ATR(1:end-1000),'--+','linewidth',2,'Color',[0.7 0.7 0.7],'MarkerIndices',1:200:length(sb.ATR(1:end-1000))); 
xlim([0 3.0])
ylabel('Average rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12)
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.7  0.7])

subplot(1,2,2)
plot(yvec/ymean,mta,'--','Color',[0 0 0],'linewidth',2); hold on
plot(yvec/ymean,mtr,':','Color',[0 0 0],'linewidth',2); hold on
plot(yvec/ymean,mtt,'-','Color',[0 0 0],'linewidth',2);  hold on
plot(yvec/ymean,mtt_hsv,'-.','Color',[0.4,0.4,0.4],'linewidth',2,'MarkerIndices',1:50:length(yvec)); hold on
plot(sb.y(1:end-1000)./ymean_mirrlees,sb.MTR(1:end-1000),'--+','linewidth',2,'Color',[0.7 0.7 0.7],'MarkerIndices',1:200:length(sb.ATR(1:end-1000)))
xlim([0 3.0])
leg = legend('Tax','Transfer','Total $t\&T$ rate','No-transfer','Mirrlees');
set(leg,'Interpreter','LaTex','Location','SouthEast','Fontsize',12)                
legend boxoff
ylabel('Marginal rates','Interpreter','LaTex','Fontsize',12)
xlabel('$y$','Interpreter','LaTex','Fontsize',12)
set(gca,'XGrid','off','YGrid','on','Fontsize',12) 
set(gca,'TickLabelInterpreter','LaTex')
ylim([-0.7  0.7])

run graph_extended.m;
saveas(gcf,'am_rates_SB_bw.pdf')
saveas(gcf,'am_rates_SB_bw','epsc')

